package com.project.ToDone;

import java.util.ArrayList;



/**
 * A class that serves as a service provider for filtering
 * the ArrayList of events
 * 
 * @author Matthew Guzdial
 *
 */

public class ToDoList {
        private ArrayList<Event> events;
        
        /**
         * Constructor that just creates a brand new ArrayList 
         * of events
         * 
         */
        public ToDoList(){
                events = new ArrayList<Event>();
        }
        
        /**
         * Constructor that takes in an ArrayList of events
         * 
         * @param events
         */
        public ToDoList(ArrayList<Event> events){
                this.events = events;
        }
        
        /**
         * Adds an event e to the ArrayList of events
         * 
         * @param e
         */
        public void addEvent(Event e){
                events.add(e);
        }
        
        /**
         * A getter for the events
         * 
         * @return
         */
        public ArrayList<Event> getEvents(){
                return events;
        }
        
        /**
         * Takes the events and returns it as a array of string's by event name
         * 
         * @return
         */
        public String[] toArrayOfStrings()
        {
                
                String[] s = new String[events.size()];
                //Log.d("lol", ""+events.size());
                int i = 0;

                for(Event e : events)
                {
                        s[i] = e.getDescriptionShort();
                        i++;
                }
                
                return s;

        }
        
        
        /**
         * Gets the events after the specific date
         * 
         * @param date The date to work with
         * @return
         */
        public ArrayList<Event> filterEventsByDateBefore(String date)
        {
                return filterEventsByDate(date, false);
        }
        
        
        /**
         * Gets the events after the specific date
         * 
         * @param date The date to work with
         * @return
         */
        public ArrayList<Event> filterEventsByDateAfter(String date)
        {
                return filterEventsByDate(date, true);
        }
        
        /**
         * Takes in a string date and figures out what which events to return
         * based upon the date and whether you want before or after
         * 
         * @param date The date to check
         * @param isAfter Determines if the events returned are before or after this date
         * @return
         */
        public ArrayList<Event> filterEventsByDate(String date, boolean isAfter){
                ArrayList<Event> newEvents = new ArrayList<Event>();
                
                int checkMonth = (int)Integer.valueOf((date.charAt(0)+""+date.charAt(1)));
                System.out.println("Check Month: " +checkMonth);
                int checkDay =(int)Integer.valueOf((date.charAt(3)+""+date.charAt(4)));
                System.out.println("Check Day: " + checkDay);
                int checkYear = (int)Integer.valueOf((date.charAt(6)+""+date.charAt(7)));
                System.out.println("Check Year: " + checkYear);
                
                for(Event e: events){
                        String eventDate = e.getDate();
                        
                        int eventMonth = (int)Integer.valueOf((eventDate.charAt(0)+""+eventDate.charAt(1)));
                        int eventDay =(int)Integer.valueOf((eventDate.charAt(3)+""+eventDate.charAt(4)));
                        int eventYear = (int)Integer.valueOf((eventDate.charAt(6)+""+eventDate.charAt(7)));
                        
                        if(e.getRecurring() && isAfter)
                        {
                        	newEvents.add(e);
                        }
                        else if(eventYear>checkYear && isAfter)
                        {
                                newEvents.add(e);
                        }
                        else if(eventYear==checkYear)
                        {
                                if(eventMonth>checkMonth && isAfter){
                                        newEvents.add(e);
                                }
                                else if(eventMonth==checkMonth){
                                        if(eventDay>=checkDay && isAfter){
                                                newEvents.add(e);
                                        }
                                        else if(eventDay<checkDay && !isAfter)
                                        {
                                        	newEvents.add(e);
                                        }
                                }
                                else if(eventMonth<checkMonth && !isAfter)
                                {
                                	newEvents.add(e);
                                }
                        }
                        else if(eventYear<checkYear && !isAfter)
                        {
                        	newEvents.add(e);
                        }
                        
                       
                }
                
                return newEvents;
        }
        
        
        
        
        /**
         * Takes in a string to filter events by type
         * 
         * 
         * @param type
         * @return
         */
        public ArrayList<Event> filterEventsByType(String type){
                ArrayList<Event> newEvents = new ArrayList<Event>();
                
                for(Event e: events)
                {
                        if(e.getType().equalsIgnoreCase(type))
                        {
                                newEvents.add(e);
                        }
                }
                return newEvents;
        }
        
        
        /**
         * Get an array of events that are Incomplete tasks
         * 
         * @return
         */
        public ArrayList<Event> filterEventsByIncompletion()
        {
                ArrayList<Event> newEvents = new ArrayList<Event>();
                
                for(Event e: events)
                {
                        if (!e.getCompleted())
                        {
                                newEvents.add(e);
                        }
                }
                return newEvents;
        }
        
        /**
         * Get an array of events that are completed already
         * @return
         */
        public ArrayList<Event> filterEventsByCompletion()
        {
                ArrayList<Event> newEvents = new ArrayList<Event>();
                
                for(Event e: events)
                {
                        if (e.getCompleted())
                        {
                                newEvents.add(e);
                        }
                }
                return newEvents;
        }
        
        /**
         * Gets string locations of events and returns as arraylist
         * 
         * @return
         */
        public ArrayList<String> getLocations(){
        	ArrayList<String> locations = new ArrayList<String>();
        	RegexTester regex = new RegexTester();
        	for(Event e: events){
        	//	if(regex.addressTest(e.getLocation())){
        			locations.add(e.getLocation());
        		//}
        	}
        	System.out.println("The event list is: " + locations.toString());
        	return locations;	
        }
        
}